package com.tyg.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tyg.entity.RoleMenu;
import com.tyg.mapper.RoleMenuMapper;
import com.tyg.service.RoleMenuService;
import com.tyg.util.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 权限（角色菜单关联）表 前端控制器
 * </p>
 *
 * @author zhangjingnan
 * @since 2022-10-22
 */
@RestController
@RequestMapping("/role-menu")
@CrossOrigin
@Slf4j
public class RoleMenuController {

    @Autowired
    RoleMenuMapper roleMenuMapper;

    @Autowired
    RoleMenuService rmService;

    //根据角色id查询此角色所有的菜单id
    @PostMapping("/getMenuListByRoleId")
    public ResultCode getMenuListByRoleId(int roleId) {
        QueryWrapper<RoleMenu> queryWrapper = new QueryWrapper<RoleMenu>();
        queryWrapper.eq("roleId", roleId);
//        List<RoleMenu> roleMenuList =roleMenuMapper.selectList(queryWrapper);
        List<RoleMenu> roleMenuList =rmService.selectList(queryWrapper);
        return ResultCode.success(roleMenuList);
    }

    //跟新角色权限菜单
    @PostMapping("/updateRoleMenu")
    public ResultCode updateRoleMenu(int roleId,int [] menuIds) {
        System.out.println("roleId:"+roleId);

        try {
            rmService.updateRoleMenu(roleId, menuIds);
            QueryWrapper<RoleMenu> queryWrapper = new QueryWrapper<RoleMenu>();
            queryWrapper.eq("roleId", roleId);
//            List<RoleMenu> roleMenuList =roleMenuMapper.selectList(queryWrapper);
            List<RoleMenu> roleMenuList =rmService.selectList(queryWrapper);
            return ResultCode.success(roleMenuList);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultCode.error(500, "更新失败！");
        }

    }



}

